<template>
    <el-aside 
    :width="$store.state.isCollapse ? '180px' : '64px'">
        <el-menu class="el-menu-vertical-demo" background-color="grey" text-color="white"
            :collapse="!$store.state.isCollapse" 
            :collapse-transition="false">
            <h3 v-show="$store.state.isCollapse">后台管理</h3>
            <h3 v-show="!$store.state.isCollapse">后台</h3>
            <el-menu-item 
            :index="item.path" v-for="item in nochildren()" :key="item.path"
            @click="clickMenu(item)">
                <component class="icons" :is="item.icon"></component>
                <span>{{ item.label }}</span>
            </el-menu-item>
            <el-sub-menu 
            :index="item.label" v-for="item in haschildren()" :key="item.path">  
                <template #title>
                    <component class="icons" :is="item.icon"></component>
                    <span>{{ item.label }}</span>
                </template>
                <el-menu-item-group>
                    <el-menu-item 
                    :index="subItem.path" v-for="(subItem, subIndex) in item.children" :key="subIndex" 
                    @click="clickMenu(subItem)">
                        <component class="icons" :is="subItem.icon"></component>
                        <span>{{ subItem.label }}</span>
                    </el-menu-item>
                </el-menu-item-group>
            </el-sub-menu>
        </el-menu>
    </el-aside>
</template>

<script>
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
    export default {
        setup() {
            const store= useStore()
            // const list = [
            //     {
            //         path: '/user',
            //         name: 'user',
            //         label: '用户管理',
            //         icon: 'user',
            //         url: 'UserManage/UserManage'
            //     },
            //     {
            //         label: '其他',
            //         icon: 'location',
            //         path: '/other',
            //         children: [
            //             {
            //                 path: '/page1',
            //                 name: 'page1',
            //                 label: '页面1',
            //                 icon: 'setting',
            //                 url: 'Other/PageOne'
            //             },
            //             {
            //                 path: '/page2',
            //                 name: 'page2',
            //                 label: '页面2',
            //                 icon: 'setting',
            //                 url: 'Other/PageTwo'
            //             }
            //         ]
            //     }
            // ];

            const nochildren = () => {
                return asyncList.filter(item => !item.children);
            };

            const haschildren = () => {
                return asyncList.filter(item => item.children);

            };

            const asyncList = store.state.menu;
            const router=useRouter();
            const clickMenu=(item)=>{
                router.push({
                    name:item.name,
                });
                //vuex管理
                store.commit('selectMenu',item);
            };
            return {
                nochildren,
                haschildren,
                clickMenu,
            };
        }
    }
</script>

<style scoped lang="less">
    .icons {
        width: 18px;
        height: 16px;
    }

    .el-menu {
        border-right: none;
        h3{
            line-height: 48px;
            color: white;
            text-align: center;
        }
    }
</style>